<?php

declare(strict_types=1);

namespace App\Common\Models;

use App\Common\Demand\Models\Admin as AdminInterfaces;
use App\Common\Exception\Store\DataNotFound;
/**
 * @property int $admin_id 
 * @property string $account 
 * @property string $password 
 * @property string $name 
 * @property string $tel 
 * @property string $email 
 * @property string $head_img 
 * @property int $is_chat 
 * @property string $last_ip 
 * @property int $last_time 
 * @property string $token 
 * @property int $error_number 
 * @property int $sort 
 * @property int $create_time 
 * @property int $update_time 
 * @property int $delete_time 
 * @property int $status 
 */
class Admin extends BaseModel implements AdminInterfaces
{
    /**
     * The table associated with the model.
     */
    protected ?string $table = 'admin';
    protected string $primaryKey = 'admin_id';
    /**
     * The attributes that are mass assignable.
     */
    protected array $fillable = ['account','password','name','tel','email','head_img','is_chat','token','error_number','sort','update_time','status'];

    /**
     * The attributes that should be cast to native types.
     */
    protected array $casts = ['admin_id' => 'integer', 'is_chat' => 'integer',  'error_number' => 'integer', 'sort' => 'integer', 'create_time' => 'integer', 'update_time' => 'integer', 'delete_time' => 'integer', 'status' => 'integer'];
    public function getListPage(array $requestData, array $field=['*']):array{
        $where = function ($q) use ($requestData) {
            $q->where('admin_id', '>', 0);
            if (isset($requestData['keyword']) && !empty($requestData['keyword'])) {
                $q->where(
                    function ($q) use ($requestData) {
                        $q->where('account', 'like', '%' . $requestData['keyword'] . '%')->orWhere('name', 'like', '%' . $requestData['keyword'] . '%')->orWhere('tel', 'like', '%' . $requestData['keyword'] . '%');
                    });

            }
        };
        $page = $this->utils->getPageLimitData($requestData['page'],$requestData['limit']);
        $order = $this->utils->getSortData($requestData['sort_field'],$requestData['sort_type'],$this->getKeyName());
        $listPage=$this->query()->where($where)->offset($page['start'])->limit($page['limit'])->orderBy($order['field'],$order['type'])->get($field);
        if($listPage->isEmpty()){
            throw new DataNotFound($this->translatorInterface->trans('admin.dataNotFind'));
        }else{
            return $listPage->toArray();
        }
    }
    
}
